package seata.demo.storage.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import seata.demo.common.dto.CommodityDTO;
import seata.demo.common.enums.RspStatusEnum;
import seata.demo.common.response.ObjectResponse;
import seata.demo.storage.entity.TStorage;
import seata.demo.storage.mappers.TStorageMapper;

/**
 * @description:
 * @author: dada
 * @date: 2020/11/24 16:16
 */
@RestController
@RequestMapping("/storage")
@Slf4j
public class StorageServiceImpl extends ServiceImpl<TStorageMapper, TStorage> {

    @PostMapping("/decreaseStorage")
    @GlobalTransactional
    @Transactional
    public ObjectResponse decreaseStorage(@RequestBody CommodityDTO commodityDTO) {

        log.info("seata全局xid={}", RootContext.getXID());

        int storage = baseMapper.decreaseStorage(commodityDTO.getCommodityCode(), commodityDTO.getCount());

        ObjectResponse<Object> response = new ObjectResponse<>();
        if (storage > 0) {
            response.setStatus(RspStatusEnum.SUCCESS.getCode());
            response.setMessage(RspStatusEnum.SUCCESS.getMessage());
            return response;
        }

        response.setStatus(RspStatusEnum.FAIL.getCode());
        response.setMessage(RspStatusEnum.FAIL.getMessage());
        return response;
    }

}
